package com.dashu.authority.servie.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dashu.authority.common.Constant;
import com.dashu.authority.common.MessageCode;
import com.dashu.authority.entity.User;
import com.dashu.authority.entity.UserAndRole;
import com.dashu.authority.exception.AuthorityException;
import com.dashu.authority.repository.UserMapper;
import com.dashu.authority.servie.UserAndRoleService;
import com.dashu.authority.servie.UserService;
import com.dashu.authority.util.AuthorityServiceUtil;
import com.dashu.authority.util.Md5Util;
import com.github.pagehelper.PageHelper;

@Service
public class UserServiceImpl extends BaseServiceImpl<User>implements UserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserAndRoleService userAndRoleService;

	@Override
	public List<User> queryAllByExample(User entity, int pageNum, int pageSize) throws AuthorityException {
		PageHelper.startPage(pageNum, pageSize);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("status", true);
		params.put("isDelete", true);
		if (entity != null) {
			if (entity.getId() != null) {
				params.put("id", entity.getId());
			}
			if (entity.getLoginId() != null && entity.getLoginId() != "") {
				params.put("loginId", entity.getLoginId());
			}
			if (entity.getStatus() != null) {
				params.put("status", entity.getStatus());
			}
			if (entity.getIsDelete() != null) {
				params.put("isDelete", entity.getIsDelete());
			}
			if (entity.getUserName() != null && entity.getUserName() != "") {
				params.put("UserName", entity.getUserName());
			}
		}
		params.put("orderBy", "create_time");
		params.put("sort", "desc");
		return userMapper.queryAllByExample(params);
	}

	@Override
	public Map<String, Object> insertEntity(User entity) throws AuthorityException {
		AuthorityServiceUtil.checkParam("用户账号", entity.getLoginId());
		AuthorityServiceUtil.checkParam("用户电话", entity.getPhoneNo());
		AuthorityServiceUtil.checkParam("用户密码", entity.getPassword());
		AuthorityServiceUtil.checkParam("用户姓名", entity.getUserName());
		entity.setPassword(Md5Util.Md5(entity.getPassword()));
		int ret = userMapper.insertEntity(entity);
		if (ret > 0) {
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		} else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
	}

	@Override
	public Map<String, Object> updateEntity(User entity) throws AuthorityException {
		AuthorityServiceUtil.checkParam("用户id", entity.getId());
		AuthorityServiceUtil.checkParam("用户账号", entity.getLoginId());
		AuthorityServiceUtil.checkParam("用户电话", entity.getPhoneNo());
		AuthorityServiceUtil.checkParam("用户密码", entity.getPassword());
		AuthorityServiceUtil.checkParam("用户姓名", entity.getUserName());
		int ret = userMapper.updateEntity(entity);
		if (ret > 0) {
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		} else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
	}

	@Override
	public Map<String, Object> deleteByIds(Integer[] ids) throws AuthorityException {
		if (ids == null || ids.length == 0) {
			throw new AuthorityException(MessageCode.ERROR_PARAMS_NULL.toString(), "用户id数组:");
		}
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("ids", ids);
		int ret = userMapper.deleteByIds(params);
		if (ret > 0) {
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_DELETE);
		} else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_DELETE);
		}
	}

	@Override
	public Map<String, Object> updateStatus(Integer[] ids, String status) throws AuthorityException {
		if (ids == null || ids.length == 0) {
			throw new AuthorityException(MessageCode.ERROR_PARAMS_NULL.toString(), "信息id数组:");
		}
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("ids", ids);
		params.put("status", status);
		int ret = userMapper.updateStatus(params);
		if (ret > 0) {
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_DELETE);
		} else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_DELETE);
		}
	}

	@Override
	public User checkUserAndPassowrd(String loginId, String password) throws AuthorityException {
		AuthorityServiceUtil.checkParam("用户账号", loginId);
		AuthorityServiceUtil.checkParam("用户密码", password);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("status", true);
		params.put("isDelete", true);
		params.put("loginId", loginId);
		params.put("password", password);
		List<User> users = userMapper.queryAllByExample(params);
		if (users != null && users.size() > 0) {
			return users.get(0);
		}
		return null;
	}

	@Override
	public User findAccountWithUserName(String loginId) throws AuthorityException {
		AuthorityServiceUtil.checkParam("用户账号", loginId);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("status", true);
		params.put("isDelete", true);
		params.put("loginId", loginId);
		List<User> users = userMapper.queryAllByExample(params);
		if (users != null && users.size() > 0) {
			return users.get(0);
		}
		return null;
	}

	@Override
	public List<User> queryAllByRoleId(Integer roleId, int pageNum, int pageSize) throws AuthorityException {
		PageHelper.startPage(pageNum, pageSize);
		List<UserAndRole> list = userAndRoleService.queryAllByRoleId(roleId);
		if (list != null && list.size() > 0) {
			List<Integer> ids = new ArrayList<Integer>();
			for (int i = 0; i < list.size(); i++) {
				ids.add(list.get(i).getUserId());
			}
			Map<Object, Object> params = new HashMap<Object, Object>();
			params.put("ids", ids);
			params.put("orderBy", "create_time");
			params.put("sort", "desc");
			return userMapper.queryAllByExample(params);
		}
		return null;
	}

	@Override
	public User findUserById(Integer id) throws AuthorityException {
		AuthorityServiceUtil.checkParam("用户id", id);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("id", id);
		List<User> list = userMapper.queryAllByExample(params);
		if (list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}

}
